package com.wfm.leetcode.editor.cn;
public class PFour8旋转图像{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    // 先水平x轴翻转，再主对角线翻转
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        // 水平翻转 x轴
        for (int i = 0; i < n / 2; ++i) {
            for (int j = 0; j < n; ++j) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - i - 1][j];
                matrix[n - i - 1][j] = temp;
            }
        }
        // 主对角线翻转 \ 主对角线
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }

    // 辅助矩阵，第i行j列，旋转90度后，变成第j行，倒数第i列
    public void rotate1(int[][] matrix) {
        int n = matrix.length;
        int[][] newMat=new int[n][n];
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                newMat[i][j] = matrix[j][n-1-i];
            }
        }
        // 赋值回源数组
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                matrix[i][j] = newMat[i][j];
            }
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new PFour8旋转图像().new Solution();
    }
}